package nz.ac.massey.cs.sdc.log4jassignment.s09007369;

import java.util.HashMap;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.stringtemplate.v4.ST;

public class StringTemplateLayout extends Layout {

	public String pattern;

	public StringTemplateLayout() {
		pattern = "";
	}

	public StringTemplateLayout(String pattern) {
		this.pattern = pattern;
	}

	public void setConversionPatterns(String pattern) {
		this.pattern = pattern;
	}

	public String getConversionPattern() {
		return pattern;
	}

	/**
	 * This allows to defer activiation of the options until all options have
	 * been set. This is required for components which have related options that
	 * remain ambigous until all are set. We leave this method empty
	 */
	@Override
	public void activateOptions() {
	}

	/**
	 * Use <conversion character> instead of %
	 */
	@Override
	public String format(LoggingEvent arg0) {

		ST stPattern = new ST(pattern);
		stPattern.add("m", arg0.getMessage());
		stPattern.add("l", arg0.getLevel());
		stPattern.add("ln", arg0.getLoggerName());
		stPattern.add("tn", arg0.getThreadName());
		stPattern.add("ts", arg0.getTimeStamp());

		return stPattern.render();
	}

	@Override
	public boolean ignoresThrowable() {
		return false;
	}

}
